/****** Object:  UserDefinedFunction [dbo].[fn_Encrypt]    Script Date: 06/09/2015 17:05:24 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_Encrypt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_Encrypt]
GO

/****** Object:  UserDefinedFunction [dbo].[fn_Encrypt]    Script Date: 06/09/2015 17:05:24 ******/
CREATE FUNCTION [dbo].[fn_Encrypt] 
(
	@plainText VARCHAR(50)
)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE
	@encryptedBinary VARBINARY(255),
	@encryptedBase64 VARCHAR(255)

SET @encryptedBinary = EncryptByPassPhrase([Global].dbo.fn_OpenSesame(), @plainText)
SET	@encryptedBase64 = CAST(N'' AS XML).value('xs:base64Binary(xs:hexBinary(sql:variable("@encryptedBinary")))', 'varchar(255)')

RETURN @encryptedBase64

END
GO